export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      ispwd: true,
      loginFormRules: {
        // 用户名的校验规则
        username: [{ required: true, message: '请输入登录名', trigger: 'blur' }, { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }],
        // 登陆密码的校验规则
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }, { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    reset() {
      this.$refs.form.resetFields()
    },
    login() {
      this.$refs.form.validate(async a => {
        if (!a) return this.$message.error('请填写正确的登录信息')
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('用户名或密码不正确')
        this.$message.success('登录成功！')
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
